package com.chilly.other.thread;

import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * CyclicBarrier 回环屏障
 * 定义:让一组线程全部达到一个状态之后再全部同时执行，
 * 特定：所有线程执行完毕之后是可以重用的
 * <p>
 * Created by Chilly Cui on 2020/12/11.
 */
public class CyclicBarrierDemoMeeting {
    private static int num = 3;
    private static CyclicBarrier cyclicBarrier = new CyclicBarrier(num, () -> {
        System.out.println("所有人都好了，开始开会...");
        System.out.println("-------------------");
    });

    private static ExecutorService executorService = Executors.newFixedThreadPool(num);

    public static void main(String[] args) throws Exception {

        executorService.submit(() -> {
            System.out.println("A 在上厕所");
            try {
                Thread.sleep(4000);
                System.out.println("A上完了");
                cyclicBarrier.await();
                System.out.println("会议结束，A退出");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        });

        executorService.submit(() -> {
            System.out.println("B 在上厕所");
            try {
                Thread.sleep(2000);
                System.out.println("B上完了");
                cyclicBarrier.await();
                System.out.println("会议结束，B退出");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        });

        executorService.submit(() -> {
            System.out.println("C 在上厕所");
            try {
                Thread.sleep(3000);
                System.out.println("C上完了");
                cyclicBarrier.await();
                System.out.println("会议结束，C退出");
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
            }
        });

        executorService.shutdown();
    }
}
